package com.katze.common;

import java.util.HashMap;

public class Message extends HashMap<String, Object> {

    private static final long serialVersionUID = -8713837118340960775L;

    private Message(){}

    public Message message(String... message) {
        if ( message.length != 0 ) {
            this.add("message", message[0]);
        }
        return this;
    }

    public Message data(Object data) {
        this.add("data", data);
        return this;
    }

    private Message code(Code code) {
        this.add("code", code.getCode());
        this.add("type", code.getType());
        return this;
    }

    public Message add(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public void reset(Code code, String... message) {
        this.add("code", code.getCode());
        this.add("type", code.getType());
        this.add("message", message.length == 1? message[0]:code.getDesc());
    }

    public static Message ok(String... message){
        return new Message().code(Code.C200).message(message);
    }

    public static Message error(String message) {
        return new Message().code(Code.C500).message(message);
    }

    public static Message exception(Code code) {
        return new Message().code(code).message(code.getDesc());
    }

    public static Message exception(Code code, String message) {
        return new Message().code(code).message(message);
    }
}
